package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"pixabay_spider/pixabay"
	"pixabay_spider/task_process"
	"pixabay_spider/utils"
	"strings"

	"github.com/gogf/gf/frame/g"
)

var gfcfgFile = flag.String("gffile", "config.yml", "the gf config file")
var gfcfgPath = flag.String("gfpath", "./etc", "the gf config file")

func main() {
	// ...
	g.Cfg().SetPath(*gfcfgPath)
	g.Cfg().SetFileName(*gfcfgFile)
	utils.LoadAccountFromExcel()

	taskServer := task_process.StartServer()
	var name string
	fmt.Println("请选择操作： 1. PIXABAY 2. EXPORT-EXCEL 3. REPAIR")
	fmt.Scanf("%s", &name)

	c := make(chan os.Signal)
	signal.Notify(c)

	if name == "1" {

		//TOdo: 暂时500页 49970个  2024/01/10

		//for i := 41; i <= 60; i++ {
		for i := 61; i <= 80; i++ {
			url := fmt.Sprintf("https://pixabay.com/videos/search/?order=ec&pagi=%d", i)
			fmt.Printf("pixabay url is %s\n", url)
			task_process.ProcessPixAbay(url)
		}

	} else if name == "2" {
		pixabay.ExportExcel()
	} else if name == "3" {
		err := task_process.RepairProcess()
		if err != nil {
			fmt.Println("RepairProcess err : ", err)
		}
	} else if name == "4" {
		//url := "https://pixabay.com/videos/search/?order=ec&pagi=5"
		//task_process.ProcessPixAbay(url)
		//
		////url := "https://pixabay.com/videos/spider-insect-web-weaver-colombia-109627/"
		////pixabay.DownloadVideos(url)

		url := "https://pixabay.com/videos/woman-sign-contract-agreement-firm-198885/"
		pixabay.GetDetail(url)

	} else if name == "5" {
		//utils.ClearDir("F:\\pixabay\\2024.01.08-2024.01.14\\ferris-wheel-amusement-park-fun-27057")

		//pixabay.Check()
		//pixabay.CheckDownload()
		//pixabay.Test()

		//str := "https://s3.eu-west-1.amazonaws.com/eu-west-1.vimeo.com/videos/668/751/668751102.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAZRUUNWVAWWO32QM7%2F20240117%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20240117T013639Z&X-Amz-Expires=86399&X-Amz-SignedHeaders=host&X-Amz-Signature=65a7e035d78147fcd1203fb7cbd5e520375b2926d31b0dbaf94ee79c5e46eafb"
		str := "https://s3.eu-west-1.amazonaws.com/eu-west-1.vimeo.com/videos/672/434/672434375.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAZRUUNWVAWWO32QM7%2F20240117%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20240117T011348Z&X-Amz-Expires=86399&X-Amz-SignedHeaders=host&X-Amz-Signature=628c3d2dc8b9cda5c1df92ee7b450c967c4d6444d074f900bb123d418337484f"

		substr := "https://s3.eu-west-1.amazonaws.com/"

		index := strings.Index(str, substr)
		if index != -1 {
			split := strings.Split(str, "?")
			filename := strings.SplitN(split[0], "/", -1)[len(strings.SplitN(split[0], "/", -1))-1]

			fmt.Println("文件名:", filename)
		} else {
			fmt.Println("字符串中不包含指定的子字符串")
		}

	} else if name == "6" {
		pixabay.CheckFolder("F:\\pixabay")

		//utils.LoadAccountFromExcel()
	} else {
		fmt.Println("输入错误")
	}

	var name1 string
	fmt.Println("程序运行结束，按任意键退出")
	fmt.Scanf("%s", &name1)
	taskServer.Stop()

}
